<template>
  <div
    class="breadcrumbBox"
    :style="{
      height: props.isScroll ? '50px' : '40px',
      'margin-top': props.isScroll ? '0' : '10px'
    }"
  >
    <el-breadcrumb class="breadcrumb" separator="/">
      <el-breadcrumb-item v-for="(item, index) in breadcrumbData" :key="item.path">
        <!-- 不可点击项 -->
        <span v-if="index === breadcrumbData.length - 1" class="no-redirect">{{
          item.meta?.title
        }}</span>
        <!-- 可点击项 且 非最后一项 -->
        <a
          v-else-if="index > 0 && index < breadcrumbData.length - 1"
          class="redirect-routine"
          @click.prevent="onLinkClick(item)"
          >{{ item.meta?.title }}</a
        >
        <a v-else class="redirect" @click.prevent="onLinkClick(item)">{{ item.meta?.title }}</a>
      </el-breadcrumb-item>
    </el-breadcrumb>
    <div class="view">
      <slot name="view"></slot>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineProps } from 'vue';
import { useRouter, useRoute, RouteRecordRaw, RouteLocationMatched } from 'vue-router';
import { ref, watch } from 'vue';

const props = defineProps({
  isScroll: {
    type: Boolean,
    default: false
  }
});
const router = useRouter();
const route = useRoute();
const breadcrumbData = ref<RouteLocationMatched[]>([]);

const getBreadcrumbData = () => {
  breadcrumbData.value = route.matched.filter(item => item.meta && item.meta.title);
};

// 监听路由变化时触发
watch(
  route,
  () => {
    getBreadcrumbData();
  },
  {
    immediate: true
  }
);

// 处理点击事件
const onLinkClick = (item: RouteLocationMatched) => {
  router.push(item.path);
};
</script>

<style lang="scss" scoped>
.breadcrumbBox {
  width: 100%;
  display: flex;
  justify-content: space-between;
  height: 40px;
  margin-top: 10px;
  margin-bottom: 4px;
  background: var(--theme-module-bg);
  align-items: center;

  .view {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
}

.breadcrumb {
  padding-left: 12px;
  box-sizing: border-box;
  .redirect {
    font-size: 14px;
    color: var(--breadcrumb-item-color);
    font-weight: 600;
  }

  .no-redirect {
    color: var(--theme-color-primary);
    font-size: 14px !important;
    font-weight: 600;
    font-family: PingFang SC;
  }
  .redirect-routine {
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #5c5c8d;
  }
}

:deep(.el-breadcrumb__separator) {
  font-weight: 300;
  color: var(--breadcrumb-item-color);
}
</style>
